/*
 * redmicro all Copyright (c)
 */

/**
 * Created by seeker910 on 2015/1/6.
 */
Rsd.define('Rsd.Faith.pages.InfoFangWuXinXiListPage', {
    extend: 'Rsd.container.Page',
    requires: [
        'Rsd.Faith.controls.XiaoQuListView',
        'Rsd.control.GridToolBar',
        'Rsd.control.PagingBar'
    ],
    layout:'border',
    items:[
        {
            region: 'west',
            xtype:'list-xiaoqu',
            border:true,
            itemClick:'item_click',
            margin: '0 10 0 0',
            width:300
        },
        {
            xtype:'grid',
            region: 'center',
            label:{
                xtype: 'grid-tool-bar',
                height:60,width:'100%',
                newButtons:[{text:'添加房屋',handler:'btn_new'}],
                searchColumns:[{name:'YeZhuXingMing',text:'业主姓名'},{name:'LouDongHao',text:'楼栋'},{name:'DanYuanHao',text:'单元'}],
                searchHandler:'loadFangWuRows'
            },
            rowdblclick:'editItem',
            footBar:{xtype:'paging-bar',indexChanged:'pageIndexChanged'},
            formFields: [

                {
                    "name": "BianHao",
                    "label": {
                        "text": "编号",
                        "width": 80
                    },
                    "index": 0,
                    "xtype": "text"
                },
                {
                    "name": "YeZhuXingMing",
                    "label": {
                        "text": "业主姓名",
                        "width": 80
                    },
                    "index": 0,
                    "xtype": "text"
                },
                {
                    "name": "XiaoQuBianHao",
                    "label": {
                        "text": "小区编号",
                        "width": 80
                    },
                    "index": 0,
                    "xtype": "text"
                },
                {
                    "name": "LouDongHao",
                    "label": {
                        "text": "楼栋号",
                        "width": 80
                    },
                    "index": 0,
                    "xtype": "text"
                },
                {
                    "name": "ShiHao",
                    "label": {
                        "text": "室号",
                        "width": 80
                    },
                    "index": 0,
                    "xtype": "text"
                },
                {
                    "name": "DanYuanHao",
                    "label": {
                        "text": "单元号",
                        "width": 80
                    },
                    "index": 0,
                    "xtype": "number"
                },
                {
                    "name": "JianZhuMianJi",
                    "label": {
                        "text": "建筑面积",
                        "width": 80
                    },
                    "index": 0,
                    "xtype": "number",
                    "format": "0.00"
                },
                {
                    "name": "FangWuLeiXing",
                    "label": {
                        "text": "房屋类型",
                        "width": 80
                    },
                    "index": 0,
                    "xtype": "text"
                },

                {
                    "name": "FangWuXingZhi",
                    "label": {
                        "text": "房屋性质",
                        "width": 80
                    },
                    "index": 0,
                    "xtype": "text"
                },

                {
                    "name": "ShouQiJiaoCunJinE",
                    "label": {
                        "text": "首期缴存金额",
                        "width": 80
                    },
                    "index": 0,
                    "xtype": "number",
                    "format": "0.00"
                },
                {
                    "name": "YuE",
                    "label": {
                        "text": "余额",
                        "width": 80
                    },
                    "dataindex": "yue",
                    "index": 0,
                    "xtype": "number",
                    "format": "0.00"
                },

                {
                    "name": "BeiYongDiZhi",
                    "label": {
                        "text": "地址",
                        "width": 80
                    },
                    "index": 0,
                    "xtype": "text"
                },

                {
                    "name": "Remark",
                    "label": {
                        "text": "备注",
                        "width": 80
                    },
                    "index": 200,
                    "xtype": "text"
                }
            ],
            columns: [
                {
                    "text": "序号",
                    "xtype": "index",
                    "width": 40,
                    "index": 0
                },
                /*{
                    "name": "XiaoQuBianHao",
                    "text": "小区编号",
                    "index": 0,
                    "width": 80,
                },*/
                {
                    //"xtype":"template",
                    "name": "BianHao",
                    "text": "房屋编号",
                    "sortable": true,
                    "index": 0,
                    "format":"format_bianhao",
                    "width": 150,
                    "nobr": true
                },
                {
                    "name": "YeZhuXingMing",
                    "text": "业主姓名",
                    "sortable": false,
                    "index": 0,
                    "width": 70,
                    "nobr": true
                },
                {
                    "name": "FangWuLeiXing",
                    "text": "房屋类型",
                    "sortable": false,
                    "index": 0,
                    "width": 70,
                    "align":"center",
                    "xtype": "enum",
                    "enum": {
                        "name": "Rsd.Faith.Core.FangWuLeiXing",
                        "valueMember": "code",
                        "items": [
                            {
                                "code": "DC",
                                "value": 0,
                                "text": "多层"
                            },
                            {
                                "code": "XGC",
                                "value": 1,
                                "text": "小高层"
                            },
                            {
                                "code": "GC",
                                "value": 2,
                                "text": "高层"
                            },
                            {
                                "code": "CGC",
                                "value": 3,
                                "text": "超高层"
                            }
                        ]
                    },
                    "nobr": true
                },
                {
                    "name": "FangWuXingZhi",
                    "text": "房屋性质",
                    "sortable": false,
                    "index": 0,
                    "width": 70,
                    "xtype": "enum",
                    "enum": {
                        "name": "Rsd.Faith.Core.FangWuXingZhi",
                        "valueMember": "code",
                        "items": [
                            {
                                "code": "SP",
                                "value": 0,
                                "text": "商品房"
                            },
                            {
                                "code": "AZ",
                                "value": 1,
                                "text": "安置房"
                            },
                            {
                                "code": "LZ",
                                "value": 2,
                                "text": "廉租房"
                            },
                            {
                                "code": "SY",
                                "value": 3,
                                "text": "商业用房"
                            }
                        ]
                    },
                    "nobr": true
                },
                {
                    "name": "LouDongHao",
                    "text": "楼栋号",
                    "sortable": true,
                    "index": 0,
                    "align":"center",
                    "width": 50,
                    "nobr": true
                },
                {
                    "name": "DanYuanHao",
                    "text": "单元号",
                    "sortable": true,
                    "index": 0,
                    "align":"center",
                    "xtype": "number",
                    "width": 50
                },
                {
                    "name": "ShiHao",
                    "text": "室号",
                    "sortable": true,
                    "index": 0,
                    "align":"center",
                    "width": 50,
                    "nobr": true
                },


                {
                    "name": "JianZhuMianJi",
                    "text": "建筑面积",
                    "sortable": false,
                    "index": 0,
                    "xtype": "number",
                    "width": 80,
                    "precision": 0
                },
                {
                    "name": "ShouQiJiaoCunJinE",
                    "text": "首次缴存金额",
                    "sortable": false,
                    "index": 0,
                    "xtype": "number",
                    "width": 100,
                    "precision": 0
                },
                {
                    "name": "BeiYongDiZhi",
                    "text": "地址",
                    "sortable": false,
                    "index": 0,
                    "width": 200,
                    "nobr": true
                },
                {
                    "name": "Remark",
                    "text": "备注",
                    "sortable": false,
                    "index": 200,
                    "width": 50,
                    "nobr": true
                }
            ]

        }
    ],
    modelType:'Rsd.Faith.Models.InfoFangWuXinXiBiaoModel',
/*
* */
    constructor: function (config) {
        var me = this;
        config = config || {};
        Rsd.apply(this, config);

        var me = this;
        var _grid = this.items[1];
        _grid.dataSource = Rsd.create('Rsd.Faith.stores.BizStore',{modelType:me.modelType});
    },
    /*
    *
    * */
    load:function () {
        var me = this;
        setTimeout(function () {
            me.items[0].loadData();
        },50);


    },
    editItem:function editItem(row)
    {
        this.openItem(row,['save','cancel']);
    },
    /*
     * */
    openItem:function openItem(record,btns,readOnly) {
        var me = this;
        //debugger;
        var _record=record;
        var _titleField = 'BianHao';
        var _sub = (_titleField== null || _titleField.length ==0) ?"titleField未设置" :(_record ? (_record[_titleField]||'-') :'*');
        var _title = ('房屋信息') +  '[' + _sub + ']';
        var _type = this.modelType;
        if(!_type.startWith('Rsd.'))
        {
            _type = Rsd.app.assembelyName + '.' + this.modelType;
        }
        Rsd.app.showModelView(_type,this.items[1].formFields,_record,_title,btns||['save','cancel'],readOnly==undefined?false:readOnly,this);
    },
    btn_new:function btn_new()
    {
        var  bian_hao= this.__xiao_qu_bian_hao;
        if(Rsd.isEmpty(bian_hao))
        {
            Rsd.alert("请选择小区");
            return;
        }
        this.openItem({XiaoQuBianHao:bian_hao},['save','cancel']);
    },
    item_click:function(item)
    {
        var me = this;

        me.__xiao_qu_bian_hao = item.content.key;
        me.loadFangWuRows();
    },
    format_bianhao:function(row)
    {
        var me = this;
        var  btn_owner = document.createElement('a');
        btn_owner.href='#';
        btn_owner.innerHTML = '产权记录';
        $(btn_owner).click (function(){
            Rsd.alert('暂无产权交易记录');
        } );

        var btn = Rsd.button('账户明细',function () {
            Rsd.app.services['biz.getlist'].requestJson({
                modelType:'Rsd.Faith.Models.BookFenHuZhangModel',
                where:[{name:'FangWuBianHao',op:'equals',value:row['BianHao']}]
            },function (data) {
                if(data.success)
                {
                    var zhanghu = data.data.rows[0];
                    Rsd.create('Rsd.Faith.dialogs.InfoFenHuZhangDialog',{data:zhanghu}).showDialog();
                }else
                {
                    Rsd.alert(data.msg);
                }

            });


        });
        return [Rsd.newLine(),btn_owner,Rsd.blankspan(),btn]
    },
    /*
    * */
    pageIndexChanged:function pageIndexChanged(opt)
    {
        var me = this;
        me.loadFangWuRows();
    },
    /*
     * */
    loadFangWuRows:function loadFangWuRows(where) {

        var me = this;
        var _args = Rsd.apply({},me.getPagingOpt());
        _args.where= where||[];
        var  bian_hao= me.__xiao_qu_bian_hao;
        if(Rsd.isEmpty(bian_hao))
        {
            Rsd.alert("请选择小区");
            return;
        }
        _args.where.push({name:'xiaoqubianhao',op:'like',value:bian_hao});
        var grid = me.items[1];
        Rsd.showWaiting(this.id,grid);
        delete _args['pageSizeList'];
        grid.loadData(_args,function(data){
            var _data = data.data;
            if(Rsd.isArray(_data))
            {
                me.setPagingOpt({pageSize:_data.length,pageIndex:0,pageCount:1,totalCount:_data.length});
            }
            else
            {
                me.setPagingOpt({pageSize:_data.pagesize,pageIndex:_data.pageindex,pageCount:_data.pagecount,totalCount:_data.total});
            }

            Rsd.closeWaiting(me.id);
        });

    },
    /**
     * @description 获取分页信息,在查询时，自动加上分页信息
     * */
    getPagingOpt:function getPagingOpt() {
        if(this.items[1].footBar instanceof  Rsd.control.PagingBar)
        {
            return this.items[1].footBar.getPaging();
        }
        return {};
    },
    /**
     * @description 设置分页信息,一般在查询条件发发生改变时，需重新设置分页 {pageIndex: 0}
     * */
    setPagingOpt:function setPagingOpt(opt) {

        if(this.items[1].footBar instanceof  Rsd.control.PagingBar)
        {
            return this.items[1].footBar.setPaging(opt);
        }
    },
});
